using System;
using System.ComponentModel;
using System.Diagnostics;
using System.Windows.Forms;
using ZoneFiveSoftware.Common.Data.Fitness;
using ZoneFiveSoftware.Common.Data.Measurement;
using ZoneFiveSoftware.Common.Visuals;
using FilteredStatistics.Common;
using FilteredStatistics.Controller;
using FilteredStatistics.Data;

namespace FilteredStatistics.View
{
    partial class ElevationCriteriaSetupDialog : Form
    {
        public ElevationCriteriaSetupDialog(IActivity activity)
        {
            InitializeComponent();

            m_Activity = activity;

            this.Text = PluginView.GetLocalizedString("SetupAltutideCriteriaText");

            ElevationUnitComboBox.Items.Add(Length.Units.Meter);
            ElevationUnitComboBox.Items.Add(Length.Units.Foot);
            ElevationUnitComboBox.SelectedItem = m_Activity != null ? m_Activity.Category.ElevationUnits : PluginMain.GetApplication().SystemPreferences.ElevationUnits;
            ElevationUnitComboBox.Format += new ListControlConvertEventHandler(DistanceUnitComboBox_Format);

            ElevationGapLabel.Text = PluginView.GetLocalizedString("AltitudeGapLabelText");
            MaxElevationLabel.Text = PluginView.GetLocalizedString("MaxAltitudeLabelText");
            MaxDistanceUnitLabel.Text = Length.LabelAbbr((Length.Units)ElevationUnitComboBox.SelectedItem);
            StartAtDistanceLabel.Text = PluginView.GetLocalizedString("StartingAtText");
            StartAtUnitLabel.Text = Length.LabelAbbr((Length.Units)ElevationUnitComboBox.SelectedItem);

            ElevationGapTextBox.Text = ((float)100).ToString("0.0");
            MaxElevationTextBox.Text = ((float)1500).ToString("0.0");
            StartAtElevationTextBox.Text = ((float)0).ToString("0.0");
        }

        void DistanceUnitComboBox_Format(object sender, ListControlConvertEventArgs e)
        {
            Length.Units item = (Length.Units)e.ListItem;

            e.Value = Length.LabelAbbr(item);
        }

#region UI callbacks

        private void OKButton_Click(object sender, EventArgs e)
        {
            DialogResult = DialogResult.OK;
            Close();
        }

        private void Cancel_Button_Click(object sender, EventArgs e)
        {
            DialogResult = DialogResult.Cancel;
            Close();
        }

        private void ElevationGapTextBox_Validating(object sender, CancelEventArgs e)
        {
            float elevationEntered;

            if (!float.TryParse(ElevationGapTextBox.Text, out elevationEntered) || elevationEntered <= 0)
            {
                MessageBox.Show(PluginView.GetLocalizedString("InvalidElevationEnteredText"),
                                PluginView.GetLocalizedString("ErrorText"),
                                MessageBoxButtons.OK, MessageBoxIcon.Error);

                e.Cancel = true;
            }
        }

        private void MaxElevationTextBox_Validating(object sender, CancelEventArgs e)
        {
            float elevationEntered;

            if (!float.TryParse(MaxElevationTextBox.Text, out elevationEntered))
            {
                MessageBox.Show(PluginView.GetLocalizedString("InvalidElevationEnteredText"),
                                PluginView.GetLocalizedString("ErrorText"),
                                MessageBoxButtons.OK, MessageBoxIcon.Error);

                e.Cancel = true;
            }
        }

        private void StartAtElevationTextBox_Validating(object sender, CancelEventArgs e)
        {
            float elevationEntered;

            if (!float.TryParse(StartAtElevationTextBox.Text, out elevationEntered))
            {
                MessageBox.Show(PluginView.GetLocalizedString("InvalidElevationEnteredText"),
                                PluginView.GetLocalizedString("ErrorText"),
                                MessageBoxButtons.OK, MessageBoxIcon.Error);

                e.Cancel = true;
            }
        }

        private void DistanceUnitComboBox_SelectedIndexChanged(object sender, EventArgs e)
        {
            MaxDistanceUnitLabel.Text = Length.LabelAbbr((Length.Units)ElevationUnitComboBox.SelectedItem);
            StartAtUnitLabel.Text = Length.LabelAbbr((Length.Units)ElevationUnitComboBox.SelectedItem);
        }

#endregion

        public IFilterCriteria GetCriteria()
        {
            if (m_PreviousCriteria is ElevationFilterCriteria)
            {
                ElevationFilterCriteria previousCriteria = m_PreviousCriteria as ElevationFilterCriteria;

                if (float.Parse(ElevationGapTextBox.Text) == previousCriteria.GapSize &&
                    float.Parse(MaxElevationTextBox.Text) == previousCriteria.MaximumElevation &&
                    float.Parse(StartAtElevationTextBox.Text) == previousCriteria.StartOffset &&
                    (Length.Units)ElevationUnitComboBox.SelectedItem == previousCriteria.ElevationUnits)
                {
                    return previousCriteria;
                }
            }

            ElevationFilterCriteria result = new ElevationFilterCriteria(m_Activity,
                                                                       float.Parse(ElevationGapTextBox.Text),
                                                                       float.Parse(MaxElevationTextBox.Text),
                                                                       float.Parse(StartAtElevationTextBox.Text),
                                                                       (Length.Units)ElevationUnitComboBox.SelectedItem);
            result.Activity = m_Activity;

            return result;
        }

        public IFilterCriteria PreviousCriteria
        {
            set
            {
                if (value is ElevationFilterCriteria)
                {
                    ElevationFilterCriteria criteria = value as ElevationFilterCriteria;

                    ElevationGapTextBox.Text = criteria.GapSize.ToString("0.0");
                    MaxElevationTextBox.Text = criteria.MaximumElevation.ToString("0.0");
                    ElevationUnitComboBox.SelectedItem = criteria.ElevationUnits;
                    StartAtElevationTextBox.Text = criteria.StartOffset.ToString("0.0");
                }

                m_PreviousCriteria = value;
            }
        }

        private IActivity m_Activity = null;
        private IFilterCriteria m_PreviousCriteria;
    }
}